package com.yonyou.pmclouds.contacttype.service.rmiimpl;

import com.alibaba.dubbo.config.annotation.Service;
import com.yonyou.pmclouds.ApplicationConsts;
import com.yonyou.pmclouds.basecom.annotation.Lock;
import com.yonyou.pmclouds.basecom.bp.processor.AuditInfoInsertProcessor;
import com.yonyou.pmclouds.basecom.bp.processor.AuditInfoUpdateProcessor;
import com.yonyou.pmclouds.basecom.bp.processor.BdReferenceChecker;
import com.yonyou.pmclouds.basecom.bp.template.SingleDeleteBpTemplate;
import com.yonyou.pmclouds.basecom.bp.template.SingleInsertBpTemplate;
import com.yonyou.pmclouds.basecom.bp.template.SingleUpdateBpTemplate;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.basecom.ref.itf.RefCodeConst;
import com.yonyou.pmclouds.basecom.ref.itf.ReferenceChecker;
import com.yonyou.pmclouds.basecom.runtime.RuntimeEnvironment;
import com.yonyou.pmclouds.basecom.util.CommonFieldConst;
import com.yonyou.pmclouds.contacttype.entity.ContactTypeVO;
import com.yonyou.pmclouds.contacttype.mapper.ContactTypeMapper;
import com.yonyou.pmclouds.contacttype.process.ContypeUniqueCheck;
import com.yonyou.pmclouds.contacttype.service.rmiitf.ContactTypeRemoteService;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

@Data
@Service(version = ApplicationConsts.APPLICATION_VERSION, interfaceClass = ContactTypeRemoteService.class)
public class ContactTypeRemoteServiceImpl implements ContactTypeRemoteService {
    @Autowired
    private ContactTypeMapper contactTypeMapper;
    @Autowired
    private ReferenceChecker referenceChecker;
    private static final String[] UPDATE_FIELDS = {CommonFieldConst.MODIFIER, CommonFieldConst.MODIFYTIME, ContactTypeVO.CONTYPE_NAME};


    @Override
    public ContactTypeVO addContactType(ContactTypeVO contactTypeVO) throws BusinessException {

        contactTypeVO.setPk_group(RuntimeEnvironment.getTenantId());
        SingleInsertBpTemplate<ContactTypeVO> bp = new SingleInsertBpTemplate<>(contactTypeMapper);
        bp.addBeforeProcessor(new ContypeUniqueCheck(contactTypeMapper));
        bp.addBeforeProcessor(new AuditInfoInsertProcessor<ContactTypeVO>());
        bp.insert(contactTypeVO);
        return contactTypeVO;
    }

    @Override
    public String deleteContactType(String pkContactType, String changeFlag) throws BusinessException {
        ContactTypeVO tmpVO = new ContactTypeVO();
        tmpVO.setPk_contacttype(pkContactType);
        tmpVO.setChangeFlag(changeFlag);
        SingleDeleteBpTemplate<ContactTypeVO> bp = new SingleDeleteBpTemplate<>(contactTypeMapper);
        bp.addBeforeProcessor(new BdReferenceChecker<ContactTypeVO>(RefCodeConst.CONTACTTYPE, referenceChecker));
        bp.addBeforeProcessor(new AuditInfoUpdateProcessor<ContactTypeVO>());
        bp.delete(tmpVO);
        return pkContactType;
    }

    @Override
    @Lock
    public ContactTypeVO updateContactType(ContactTypeVO contactTypeVO) throws BusinessException {

        SingleUpdateBpTemplate<ContactTypeVO> bp = new SingleUpdateBpTemplate<>(contactTypeMapper, UPDATE_FIELDS);
        bp.addBeforeProcessor(new ContypeUniqueCheck(contactTypeMapper));
        bp.addBeforeProcessor(new AuditInfoUpdateProcessor<ContactTypeVO>());
        bp.update(contactTypeVO);
        return contactTypeVO;
    }

    @Override
    public List<ContactTypeVO> selectByPkGroup(String pk_group) {
        return contactTypeMapper.selectByPkGroup(pk_group);

    }

    @Override
    public List<ContactTypeVO> queryConTypesByPK(String[] pk_contypes) {
        List<ContactTypeVO> resultList = new ArrayList<ContactTypeVO>();
        resultList = contactTypeMapper.selectByIds(pk_contypes);
        return resultList;
    }


}
